package com.sx.micowell.framework.view.chartManager

import android.graphics.Color
import android.graphics.Matrix
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import com.github.mikephil.charting.charts.LineChart
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.components.YAxis
import com.github.mikephil.charting.data.Entry
import com.github.mikephil.charting.data.LineData
import com.github.mikephil.charting.data.LineDataSet
import com.github.mikephil.charting.formatter.IndexAxisValueFormatter
import com.sx.micowell.R

/**
 * @Description
 * @Author 梁浩文
 * @Date 2021/8/23 10:04
 */
//fun LineChart.test() {
//    var fullDrawable = GradientDrawable()
//    fullDrawable.shape = GradientDrawable.RECTANGLE
//    fullDrawable.colors = intArrayOf(
//        Color.parseColor("#D2F8EA"),
//        Color.parseColor("#D2F8EA"),
//        Color.parseColor("#E5FBF3"),
//        Color.parseColor(
//            "#FEFFFE"
//        ),
//        Color.WHITE
//    )
//    setTestData(fullDrawable, UI(), xAxis.UI_XAxis(), axisLeft.UI_YAxis())
//
//}
//inline fun LineChart.setTestData(fillDrawable: Drawable, ui: Unit, xAxis: Unit, yAxis: Unit) {
//    ui
//    xAxis
//    yAxis
//    var arrays = ArrayList<Entry>()
//    for (i in 0..30) {
//        arrays.add(Entry(i.toFloat(), (0..10).random().toFloat()))
//    }
//    this.data = LineData(setNetData(fillDrawable, arrays))
//    scaleX(arrays.size)
////    getXAxis().valueFormatter=IndexAxisValueFormatter()
////    axisLeft.valueFormatter=IndexAxisValueFormatter()
//}


fun LineChart.initData(fillDrawable: Drawable, arrays: List<Entry>, xLable: Array<String>? = null) {
    this.data = LineData(setNetData(fillDrawable, arrays))
    this.xAxis.axisMinimum=0f
        xLable?.let {
//            this.xAxis.labelCount=it.size
            this.xAxis.valueFormatter = IndexAxisValueFormatter(it)
            scaleX_OBJ(arrays.size)
        }
    invalidate()

}



fun LineChart.initUi(){
    UI()
    xAxis.UI_XAxis()
    axisLeft.UI_YAxis()
}


fun LineChart.setNetData(fillDrawable: Drawable?=null, arrays: List<Entry>): LineDataSet {
    var dataSet = LineDataSet(arrays, null)
    dataSet.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);//曲线风格
    dataSet.setCubicIntensity(0.9f);//设置曲线的平滑度
    fillDrawable?.let {
        dataSet.fillDrawable = fillDrawable//曲线填充颜色
    }
    dataSet.setDrawFilled(true)//曲线允许填充
    dataSet.color = Color.parseColor("#00DA8B")//曲线颜色
    dataSet.fillAlpha=255
    dataSet.setDrawValues(false)//取消画值
    dataSet.setDrawCircles(false)
    dataSet.setDrawCircleHole(false)
    dataSet.setLineWidth(resources.getDimension(R.dimen.dp_1));//曲线宽度
    dataSet.setDrawHorizontalHighlightIndicator(false)
    dataSet.setmDrawHollowPointIndicator(true)//空心圆
    dataSet.highLightColor = Color.parseColor("#00DA8B")
    dataSet.highlightLineWidth = resources.getDimension(R.dimen.dp_1)
    return dataSet;
}

/**
 * 默认宽度显示10条
 *
 * @param count
 */
fun LineChart.scaleX_OBJ(count: Int) {
    if (count > 6) {
        val scale = count.toFloat() / 6.toFloat()
        val matrix = Matrix()
        matrix.postScale(scale, 0f)
        viewPortHandler.refresh(matrix, this, false)
    }
}